from django.shortcuts import render
from .sers import *
from rest_framework.response import Response
from rest_framework.views import APIView
import datetime
from decimal import Decimal
from django.db.models import F
# Create your views here.



class LoginView(APIView):
    def post(self,request):
        name = request.data.get('name')
        id = request.data.get('id')
        data = UserModel.objects.filter(name=name).first()
        if data:
            return Response({'msg':'进店','code':200,'id':data.id,'name':data.name})
        else:
            return Response({'msg':'未进店'})

class EatView(APIView):
    def get(self,request):
        id = request.GET.get('id')
        data = EatModel.objects.filter(id=id)
        ser = EatSers(data,many=True)
        return Response(ser.data)

class ShowView(APIView):
    def get(self,request):
        data = EatModel.objects.all()
        xin = EatSers(data,many=True)
        return Response(xin.data)

class StartView(APIView):
    def post(self,request):
        id = request.data.get('id')
        uid = request.data.get('uid')
        TimeModel.objects.create(user_id=id,boss_id=uid)
        return Response({'msg':'开始计时','code':200})

    def get(self,request):
        data = TimeModel.objects.all()
        xin = TimeSers(data,many=True)
        return Response(xin.data)

class EndView(APIView):
    def put(self,request):
        id = request.data.get('id')
        uid = request.data.get('uid')
        user = UserModel.objects.filter(id = id)
        data = TimeModel.objects.filter(user_id=id,boss_id=uid)
        start = data.start
        end = datetime.datetime.now()
        time = end-start
        hours = Decimal(time.days * 24 + time.seconds / 3600)
        if user.type==False:
            prices = hours * 500
            if prices>3000:
                prices = prices * 0.8
            elif prices > 5000:
                prices = prices * 0.5
        else:
            prices = hours * 200
            if prices>3000:
                prices = prices * 0.8
            elif prices > 5000:
                prices = prices * 0.5
        prices = Decimal(prices)
        BossModel.objects.filter(id=uid).updata(price=F('price')+prices)
        UserModel.objects.filter(id=id).updata(money = F('money')+prices)

        data.end = end
        data.money = prices
        data.count+=1

        data.save()
        return Response({'msg':'吃完了'})

class Sou1View(APIView):
    def get(self,request):
        data = TimeModel.objects.filter(max(TimeModel.count))
        xin = TimeListSers(data,max(True))
        return Response(xin.data)

class Sou2View(APIView):
    def get(self,request):
        id = request.GET.get('id')
        user = UserModel.objects.filter(id=id)
        data = TimeModel.objects.filter(max(user.money))
        xin = TimeListSers(data, max(True))
        return Response(xin.data)


class BossView(APIView):
    def post(self,request):
        data = BossModel.objects.all()
        xin = BossSers(data,many=True)
        return Response(xin.data)

class Login2(APIView):
    def post(self,request):
        name = request.data.get('name')
        data = BossModel.objects.first(name=name)
        if data:
            return Response({'code':200,'msg':'登陆成功'})




















